#include <QtGui>
#include <QApplication>
#include "../include/ros_robot/main_window.hpp"

std::string Command(const std::string &cmd){
    FILE *pipe=nullptr;
    if((pipe=popen(cmd.c_str(),"r"))==nullptr){
        return "command failed";
    }
    char result[512]="";
    fgets(result,sizeof(result),pipe);
    pclose(pipe);
    return result;
}
int main(int argc, char **argv) {

    system("roscore&");//add
    sleep(2);//add//roscore启动需要时间

    QApplication app(argc, argv);
    ros_robot::MainWindow w(argc,argv);
    w.show();
    app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
    int result = app.exec();
    
    std::string cmd("ps -C roscore -o pid=");
    std::string command_result=Command(cmd);

    std::string kill_process="kill ";
    kill_process.append(command_result);

    std::string cmd2(kill_process);
    std::string command_result2=Command(cmd2);

    return result;
}
